#include <sys_config.h>

#if 0
#ifdef HILINK_SUPPORT
#include <types.h>
#include <osal/osal.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libpub/lib_frontend.h>

#include "osdobjs_def.h"
#include "osd_config.h"
#include "string.id"
#include "images.id"
#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"

#include <hld/ali_3g_mdm/ali_3g_mdm_api.h>
#include <api/libhilink/lib_hilink.h>
#include "copper_common/com_api.h"



#ifdef	_BUILD_OTA_E_
#define WIN_SH_IDX	  	WSTL_WINSEARCH_03_HD
#else
#define WIN_SH_IDX	  	WSTL_WIN_BODYRIGHT_01_HD
#endif

#define CON_SH_IDX   WSTL_BUTTON_01_HD
#define CON_HL_IDX   WSTL_BUTTON_05_HD
#define CON_SL_IDX   WSTL_BUTTON_01_HD
#define CON_GRY_IDX  WSTL_BUTTON_07_HD

#define TXT_SH_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  WSTL_BUTTON_07_HD

#define SEL_SH_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_HL_IDX   WSTL_BUTTON_04_HD
#define SEL_SL_IDX   WSTL_BUTTON_01_FG_HD
#define SEL_GRY_IDX  WSTL_BUTTON_07_HD

#define EDF_SH_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_HL_IDX   WSTL_BUTTON_04_HD
#define EDF_SL_IDX   WSTL_BUTTON_01_FG_HD
#define EDF_GRY_IDX  WSTL_BUTTON_07_HD

#ifndef SD_UI
#ifdef	_BUILD_OTA_E_
#define	W_W         692             //493
#define	W_H         488             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         98              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 48)
#else

#ifndef SUPPORT_CAS_A
/* define in win_com_menu_define.h already */
#define	W_L     	248
#define	W_T     	98
#define	W_W     	692
#define	W_H     	488
#endif

#define CON_L		(W_L+20)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 60)
#endif

//#define CON_L		W_L


#define CON_H		40
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		260
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2
#define LIST_ITEMCNT 7
#else
#ifdef	_BUILD_OTA_E_
#define	W_W         482             //493
#define	W_H         360             //320
#define	W_L         GET_MID_L(W_W)  //60
#define	W_T         60              //GET_MID_T(W_H)//60
#define CON_L		(W_L + 24)
#define CON_T		(W_T + 4)
#define CON_W		(W_W - 40)
#else
#define	W_L     	105//384
#define	W_T     	57//138
#define	W_W     	482
#define	W_H     	370
#define CON_L		(W_L+20)
#define CON_T		(W_T + 8)
#define CON_W		(W_W - 40)
#endif

#define CON_H		30
#define CON_GAP		8

#define TXT_L_OF  	10
#define TXT_W  		180
#define TXT_H		CON_H
#define TXT_T_OF	((CON_H - TXT_H)/2)

#define SEL_L_OF  	(TXT_L_OF + TXT_W)
#define SEL_W  		(CON_W-TXT_W-20)
#define SEL_H		CON_H
#define SEL_T_OF	((CON_H - SEL_H)/2)

#define LINE_L_OF	0
#define LINE_T_OF  	(CON_H+2)
#define LINE_W		CON_W
#define LINE_H     	2
#define LIST_ITEMCNT 7

#endif

extern CONTAINER g_win_hilink_traffic;

extern CONTAINER g_win_hilink_download_traffic_con;
extern CONTAINER g_win_hilink_upload_traffic_con;
extern CONTAINER g_win_hilink_total_traffic_con;

extern TEXT_FIELD hilink_download_traffic;
extern TEXT_FIELD hilink_upload_traffic;
extern TEXT_FIELD hilink_total_traffic;

extern TEXT_FIELD hilink_download_traffic_edit;
extern TEXT_FIELD hilink_upload_traffic_edit;
extern TEXT_FIELD hilink_total_traffic_edit;

extern TEXT_FIELD w_hilink_traffic_line0;
extern TEXT_FIELD w_hilink_traffic_line1;
extern TEXT_FIELD w_hilink_traffic_line2;

static VACTION win_hilink_traffic_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT win_hilink_traffic_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static PRESULT win_hilink_traffic_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION win_hilink_traffic_item_con_keymap(POBJECT_HEAD pObj, UINT32 key);

//static VACTION win_hilink_traffic_edf_keymap(POBJECT_HEAD pObj, UINT32 key);
//static PRESULT win_hilink_traffic_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#define LDEF_CON(root, varCon,nxtObj,ID,IDl,IDr,IDu,IDd,l,t,w,h,hl,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,IDl,IDr,IDu,IDd, l,t,w,h, CON_SH_IDX,hl,CON_SL_IDX,CON_GRY_IDX,   \
    win_hilink_traffic_item_con_keymap,win_hilink_traffic_item_con_callback,  \
    conobj, ID,1)

/*
#define LDEF_EDIT(root, varNum, nxtObj, ID, l, t, w, h,style,max_len,cursormode,pat,pre,sub,str)	\
    DEF_EDITFIELD(varNum,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, EDF_SH_IDX,EDF_HL_IDX,EDF_SL_IDX,EDF_GRY_IDX,   \
    win_hilink_traffic_edf_keymap,win_hilink_traffic_edf_callback,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 30,0,style,pat,max_len,cursormode,pre,sub,str)
*/
#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)
    
#define LDEF_TXT_EXT(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)    
/*
#define LDEF_TXT_BTN(root,varTxt,nextObj,varLine,ID,IDu,IDd,l,t,w,h,resID)		\
    DEF_TEXTFIELD(varTxt,&root,nextObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    otaset_btn_keymap,otaset_btn_callback,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, TXT_L_OF,0,resID,NULL)\
	LDEF_LINE(&root,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF + 10,LINE_W,LINE_H,0,0)
*/

#define LDEF_MM_ITEM(root,varCon,nxtObj,varTxt,varTxt_edit,varLine,ID,IDu,IDd,\
						l,t,w,h,resID,str) \
    LDEF_CON(&root,varCon,nxtObj,ID,ID,ID,IDu,IDd,CON_L,t,CON_W,CON_H,CON_HL_IDX,&varTxt,ID)   \
    LDEF_TXT(&varCon,varTxt,&varTxt_edit,l + TXT_L_OF,t + TXT_T_OF,TXT_W,TXT_H,resID,NULL)    \
    LDEF_TXT_EXT(&varCon,varTxt_edit,NULL,l + SEL_L_OF ,t + SEL_T_OF,SEL_W,SEL_H,NULL,str)   \
    LDEF_LINE(&varCon,varLine,NULL,1,1,1,1,1,l + LINE_L_OF, t + LINE_T_OF,LINE_W,LINE_H,0,0)


#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
    win_hilink_traffic_keymap,win_hilink_traffic_callback,  \
    nxtObj, focusID,0)

UINT16 hilink_download_traffic_strs[30] = {0};
UINT16 hilink_upload_traffic_strs[30] = {0};
UINT16 hilink_total_traffic_strs[30] = {0};


//UINT8 hilink_traffic_len = 20;

//static char traffic_pattern[] = "r0~999999999";

LDEF_MM_ITEM(g_win_hilink_traffic,g_win_hilink_download_traffic_con,&g_win_hilink_upload_traffic_con,hilink_download_traffic,hilink_download_traffic_edit,w_hilink_traffic_line0,1,3,2,\
			CON_L, CON_T + (CON_H + CON_GAP)*0, CON_W, CON_H, RS_HILINK_DOWNLOAD_TRAFFIC, hilink_download_traffic_strs)

LDEF_MM_ITEM(g_win_hilink_traffic,g_win_hilink_upload_traffic_con,&g_win_hilink_total_traffic_con,hilink_upload_traffic,hilink_upload_traffic_edit,w_hilink_traffic_line1,2,1,3,\
			CON_L, CON_T + (CON_H + CON_GAP)*1, CON_W, CON_H, RS_HILINK_UPLOAD_TRAFFIC, hilink_upload_traffic_strs)

LDEF_MM_ITEM(g_win_hilink_traffic,g_win_hilink_total_traffic_con,NULL,hilink_total_traffic,hilink_total_traffic_edit,w_hilink_traffic_line2,3,2,1,\
			CON_L, CON_T + (CON_H + CON_GAP)*2, CON_W, CON_H, RS_HILINK_TOTAL_TRAFFIC, hilink_total_traffic_strs)

LDEF_WIN(g_win_hilink_traffic,&g_win_hilink_download_traffic_con,W_L, W_T,W_W,W_H,1)


static VACTION win_hilink_traffic_keymap(POBJECT_HEAD pObj, UINT32 key)
{
    VACTION act;
    
    switch(key)
    {
    case V_KEY_UP:
        act = VACT_CURSOR_UP;
        break;
    case V_KEY_DOWN:
        act = VACT_CURSOR_DOWN;
        break;      
    case V_KEY_EXIT:
    case V_KEY_MENU:
        act = VACT_CLOSE;
        break;
    default:    
        act = VACT_PASS;
        break;
    }
    
    return act;
}

void win_hilink_traffic_init(void)
{
    char str1[10]="6666";
    char str2[10]="3333";
    char str3[10]="9999";

	OSD_SetTextFieldContent(&hilink_download_traffic_edit, STRING_ANSI, (UINT32)str1);
    OSD_SetTextFieldContent(&hilink_upload_traffic_edit, STRING_ANSI, (UINT32)str2);
	OSD_SetTextFieldContent(&hilink_total_traffic_edit, STRING_ANSI, (UINT32)str3);

}


static PRESULT win_hilink_traffic_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact;

    switch(event)
    {
    case EVN_PRE_OPEN:
        win_hilink_traffic_init();
        wincom_open_title((POBJECT_HEAD)&g_win_hilink_traffic,RS_HILINK_TRAFFIC, 0);
        break;
	case EVN_POST_OPEN:
		break;		
    case EVN_PRE_CLOSE:
        /* Make OSD not flickering */
        *((UINT32*)param2) &= ~C_CLOSE_CLRBACK_FLG; 
        break;
	case EVN_POST_CLOSE:
        break;
    case EVN_MSG_GOT:
        if ((param1 ==  CTRL_MSG_SUBTYPE_CMD_EXIT) ||
                (param1 ==  CTRL_MSG_SUBTYPE_CMD_EXIT_ALL))
        {
            ret = PROC_LEAVE;
        }    
        if ((param1 == CTRL_MSG_SUBTYPE_CMD_HILINK)&&(param2 == HILINK_STATUS_PLUGOUT))
        {
            ret = PROC_LEAVE;
            ap_send_msg(CTRL_MSG_SUBTYPE_CMD_HILINK, HILINK_STATUS_PLUGOUT, FALSE);
        }
        break;
    default:
        break;
    }

    return ret; 
}

static VACTION win_hilink_traffic_item_con_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	switch(key)
	{
	case V_KEY_ENTER:     
		act = VACT_ENTER;
		break; 
	default:
		act = VACT_PASS;
	}

	return act;
}

static PRESULT win_hilink_traffic_item_con_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
    PRESULT ret = PROC_PASS;
    VACTION unact;

	switch(event)
	{
	case EVN_UNKNOWN_ACTION:
		unact = (VACTION)(param1 >> 16);

		break;
	default:
		break;
	}
    return ret;
}

/*

static VACTION win_hilink_traffic_edf_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	
	switch(key)
    {
	case V_KEY_0:	case V_KEY_1:	case V_KEY_2:	case V_KEY_3:
	case V_KEY_4:	case V_KEY_5:	case V_KEY_6:	case V_KEY_7:
	case V_KEY_8:	case V_KEY_9:
		act = key - V_KEY_0 + VACT_NUM_0;
		break;
	case V_KEY_LEFT:
		act = VACT_EDIT_LEFT;
		break;
	case V_KEY_RIGHT:
		act = VACT_EDIT_RIGHT;
		break;		
	default:					
		act = VACT_PASS;
	}

	return act;	 
}

enum
{
    IDC_HILINK_DOWNLOAD_TRAFFIC = 1,
	IDC_HILINK_UPLOAD_TRAFFIC,
    IDC_HILINK_TOTAL_TRAFFIC,
};

static PRESULT win_hilink_traffic_edf_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	UINT8 status;
	EDIT_FIELD* edf;
	UINT8 back_save;
	UINT32 value;

	bID = OSD_GetObjID(pObj);

	edf = (EDIT_FIELD*)pObj;
	//libc_printf("pObj=%xh, event=%x, param1=%x, param2=%x\n",pObj, event, param1, param2);

	switch(event)
	{
	case EVN_KEY_GOT:
		if((bID == IDC_HILINK_DOWNLOAD_TRAFFIC) || 
		   (bID == IDC_HILINK_UPLOAD_TRAFFIC) ||
		   (bID == IDC_HILINK_TOTAL_TRAFFIC)) 
			ret = mobile_input_proc(edf,(VACTION)(param1>>16), param1 & 0xFFFF, param2);
		break;
		
	case EVN_PRE_CHANGE:
		break;
	case EVN_POST_CHANGE:
		//libc_printf("POST: ip %xh, get value = %xh\n",param1,OSD_GetEditFieldContent((PEDIT_FIELD)pObj));
		break;

	case EVN_FOCUS_PRE_GET:
	case EVN_PARENT_FOCUS_PRE_GET:
        //mobile_input_init(&edit_hilink_password, NULL);
		//netupg_mobile_input_init(bID);
		break;

	case EVN_FOCUS_PRE_LOSE:
	case EVN_PARENT_FOCUS_PRE_LOSE:	
		
		//netupg_mobile_input_init(bID);
		break;		

	case EVN_DATA_INVALID:		
		win_compopup_init(WIN_POPUP_TYPE_OK);
        win_compopup_set_msg("Invalid address number!", NULL, 0);
		win_compopup_open_ext(&back_save);
		ret = PROC_LOOP;
		break;
	}
	return ret;
}
*/
#endif
#endif
